package com.jianli.smart.core;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.jianli.smart.core.utils.ReflectionUtil;

public final class BeanHelper {
	private static final Map<Class<?>, Object> BEAN_MAP = new HashMap<Class<?>, Object>();
	
	static{
		Set<Class<?>> beanClassSet = ClassHelper.getBeanSet();
		for(Class<?> beanClass:beanClassSet){
			Object object = ReflectionUtil.newInstance(beanClass);
			BEAN_MAP.put(beanClass, object);
		}
	}
	
	public static Map<Class<?>, Object> getBeanMap(){
		return BEAN_MAP;
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T getBean(Class<T> cls){
		if(BEAN_MAP.containsKey(cls)){
			return (T) BEAN_MAP.get(cls);
		}else{
			throw new RuntimeException("can not get bean by class"+cls);
		}
		
	}
	
	public static void setBean(Class<?> cls, Object obj){
		if(BEAN_MAP.containsKey(cls)){
			System.out.println(cls.getName()+"原先："+BEAN_MAP.get(cls)+"覆盖："+obj);
		}
		BEAN_MAP.put(cls, obj);
	}
}
